from django.db import models

from oracle.base.basemodel import BaseModel
from oracle.models import OUserInfo as OracleClockTower


class School(BaseModel):
    name = models.CharField("学校名称", max_length=32)


class Department(BaseModel):
    name = models.CharField("部门名称", max_length=32)


class Gender(models.IntegerChoices):
    unknown = 0, "未知"
    male = 1, "男"
    female = 2, "女"


class UserInfo(OracleClockTower):
    """
    用户表
    """
    nickname = models.CharField("姓名", max_length=32)
    phone = models.CharField("手机号", max_length=32)
    gender = models.IntegerField("性别", choices=Gender.choices, default=1)
    depart = models.ForeignKey(Department, verbose_name="部门", on_delete=models.CASCADE, blank=True, null=True)

    def __str__(self):
        return self.nickname


class Course(BaseModel):
    """
    课程表
    """
    name = models.CharField("课程名称", max_length=32)


class ClassList(BaseModel):
    """
    班级表
    """
    school = models.ForeignKey(School, verbose_name="校区", on_delete=models.CASCADE)
    course = models.ForeignKey(Course, verbose_name="课程名称", on_delete=models.CASCADE)
    semester = models.PositiveIntegerField("班级(期)")
    price = models.PositiveIntegerField("学费")
    start_date = models.DateField("开班日期")
    graduate_date = models.DateField("结业日期", null=True, blank=True)
    class_teacher = models.ForeignKey(UserInfo, verbose_name="班主任", on_delete=models.CASCADE, related_name="classes",
                                      limit_choices_to={"depart__name": "教质部", })
    tech_teachers = models.ManyToManyField(UserInfo, verbose_name="任课老师", related_name="tech_classes", blank=True,
                                           limit_choices_to={"depart__name__contains": "教学部", })
    memo = models.TextField("说明", blank=True, null=True)

    custom_model_name = "class"

    def __str__(self):
        return f"{self.course.name}({self.semester}期)"


class ClassStatus(models.IntegerChoices):
    sign_up = 1, "已报名"
    un_sign_up = 2, "未报名"


class ClassSource(models.IntegerChoices):
    qq_group = 1, "qq群"
    neibujieshao = 2, "内部转介绍"
    guangwang = 3, "官方网站"
    baidutuiguang = 4, "百度推广"
    tuiguang360 = 5, "360推广"
    sougoutuiguang = 6, "搜狗推广"
    tengxunketang = 7, "腾讯课堂"
    guandiantong = 8, "广点通"
    gaoxiaoxuanjiang = 9, "高校宣讲"
    qudaodaili = 10, "渠道代理"
    web51CTO = 11, "51CTO"
    zhihuitui = 12, "智汇推"
    wangmeng = 13, "网盟"
    dsp = 14, "DSP"
    seo = 15, "SEO"
    other = 16, "其他"


class Customer(BaseModel):
    """
    客户表
    """
    MAX_PRIVATE_CUSTOM_COUNT = 2

    qq = models.CharField("联系方式", max_length=64, unique=True, help_text="QQ号/微信号/手机号")
    name = models.CharField("姓名", max_length=16)
    class_status = models.IntegerField("状态", choices=ClassStatus.choices, default=2, help_text="选择客户此时的状态")
    gender = models.SmallIntegerField("性别", choices=Gender.choices)
    source = models.SmallIntegerField("客户来源", choices=ClassSource.choices, default=1)
    referral_from = models.ForeignKey("self", verbose_name="转介绍学员", blank=True, null=True, on_delete=models.CASCADE,
                                      help_text="若此客户是转介绍自内部学员，请在此处选择内部学员姓名", related_name="internal_referral")
    course = models.ManyToManyField(Course, verbose_name="咨询课程")
    consultant = models.ForeignKey(UserInfo, verbose_name="课程顾问", related_name="consultant", blank=True, null=True,
                                   on_delete=models.CASCADE, limit_choices_to={"depart__name": "销售部"})
    education_choices = (
        (1, "重点大学"),
        (2, "普通本科"),
        (3, "独立院校"),
        (4, "民办本科"),
        (5, "大专"),
        (6, "民办专科"),
        (7, "高中"),
        (8, "其他"),
    )
    education = models.IntegerField("学历", choices=education_choices, blank=True, null=True)
    graduation_school = models.CharField("毕业学校", max_length=64, blank=True, null=True)
    major = models.CharField("所学专业", max_length=64, blank=True, null=True)
    experience_choices = [
        (1, "在校生"),
        (2, "应届毕业生"),
        (3, "半年以内"),
        (4, "半年至一年"),
        (5, "一年至三年"),
        (6, "三年至五年"),
        (7, "五年以上"),
    ]
    experience = models.IntegerField("工作经验", choices=experience_choices, blank=True, null=True)
    work_status_choices = [
        (1, "在职"),
        (2, "无业"),
    ]
    work_status = models.IntegerField("职业状态", choices=work_status_choices, default=1, blank=True, null=True)
    company = models.CharField("目前就职公司", max_length=64, blank=True, null=True)
    salary = models.CharField("目前薪资", max_length=64, blank=True, null=True)

    date = models.DateField("咨询日期", auto_now_add=True)
    last_consult_date = models.DateField("最后跟进日期", auto_now_add=True)

    def __str__(self):
        return f"姓名:{self.name},联系方式:{self.qq}"


class ConsultRecord(BaseModel):
    """
    客户跟进记录
    """
    title = models.CharField("记录标题", max_length=32)
    customer = models.ForeignKey(Customer, verbose_name="咨询客户", on_delete=models.CASCADE)
    consultant = models.ForeignKey(UserInfo, verbose_name="跟踪人", on_delete=models.CASCADE)
    date = models.DateTimeField("跟进日期", auto_now=True)
    note = models.TextField("跟进内容", null=True, blank=True)
    link_name = models.CharField("链接地址", max_length=128, null=True, blank=True)
    link_title = models.CharField("链接标题", max_length=128, null=True, blank=True)
    icon = models.CharField("显示图标", null=True, blank=True, max_length=16)


class PaymentRecord(BaseModel):
    """
    缴费申请
    """
    customer = models.ForeignKey(Customer, verbose_name="客户", on_delete=models.CASCADE)
    consultant = models.ForeignKey(UserInfo, verbose_name="课程顾问", on_delete=models.CASCADE, help_text="谁签的单就选谁")
    pay_type_choice = [
        (1, "报名费"),
        (2, "学费"),
        (3, "退学"),
        (4, "其他"),
    ]
    pay_type = models.IntegerField("费用类型", choices=pay_type_choice, default=1)
    paid_fee = models.PositiveIntegerField("金额", default=0)
    class_list = models.ForeignKey(ClassList, verbose_name="申请班级", on_delete=models.CASCADE, null=True, blank=True)
    apply_date = models.DateTimeField("申请日期", auto_now_add=True)
    confirm_status_choices = (
        (1, "申请中"),
        (2, "已确认"),
        (3, "已驳回"),
    )
    confirm_status = models.IntegerField("确认状态", choices=confirm_status_choices, default=1)
    confirm_date = models.DateTimeField("确认日期", null=True, blank=True)
    confirm_user = models.ForeignKey(
        UserInfo, verbose_name="审批人", on_delete=models.CASCADE, related_name="confirms", null=True, blank=True)
    note = models.TextField("备注", blank=True, null=True)


class Student(BaseModel):
    """
    学生表
    """
    customer = models.OneToOneField(Customer, models.CASCADE, "key", verbose_name="客户信息", db_column="customer_key")
    qq = models.CharField("QQ号", max_length=32)
    mobile = models.CharField("手机号", max_length=32)
    emergency_contract = models.CharField("紧急联系人电话", max_length=32)
    class_list = models.ManyToManyField(ClassList, verbose_name="已报班级", blank=True)
    student_status_choices = [
        (1, "申请中"),
        (2, "在读"),
        (3, "毕业"),
        (4, "退学"),
    ]
    student_status = models.IntegerField("学员状态", choices=student_status_choices, default=1)
    score = models.IntegerField("积分", default=100)
    memo = models.CharField("备注", max_length=255, blank=True, null=True)

    def __str__(self):
        return self.customer.name


class ScoreRecord(BaseModel):
    """
    积分记录
    """
    student = models.ForeignKey(Student, verbose_name="学生", on_delete=models.CASCADE, to_field="key")
    content = models.TextField("理由")
    score = models.IntegerField("分值", help_text="违纪扣分写负值，表现优异加分写正值")
    user = models.ForeignKey(UserInfo, verbose_name="执行人", on_delete=models.CASCADE, to_field="key")


class CourseRecord(BaseModel):
    """
    上课记录表
    """
    class_object = models.ForeignKey(ClassList, verbose_name="班级", on_delete=models.CASCADE, to_field="key")
    day_num = models.IntegerField("节次")
    teacher = models.ForeignKey(UserInfo, verbose_name="讲师", on_delete=models.CASCADE, to_field="key")
    date = models.DateField("上课日期", auto_now_add=True)

    # course_title = models.CharField("本节课程标题", max_length=64, blank=True, null=True)
    # course_memo = models.TextField("本节课程内容概要", blank=True, null=True)
    # homework_title = models.CharField("作业标题", max_length=64, blank=True, null=True)
    # homework_memo = models.TextField("作业描述", blank=True, null=True)
    # exam = models.TextField("踩分点", blank=True, null=True)

    def __str__(self):
        return f"{self.class_object} day{self.day_num}"


class StudyRecord(BaseModel):
    """
    学生考勤记录
    """
    course_record = models.ForeignKey(CourseRecord, verbose_name="第几天课程", on_delete=models.CASCADE, to_field="key")
    student = models.ForeignKey(Student, verbose_name="学生", on_delete=models.CASCADE, to_field="key")
    record_choices = (
        ("checked", "已签到"),
        ("vacate", "请假"),
        ("late", "迟到"),
        ("noshow", "缺勤"),
        ("leave_early", "早退"),
    )
    record = models.CharField("上课记录", choices=record_choices, default="checked", max_length=64)
